package com.cauli.manage.menu.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import com.cauli.manage.menu.model.dto.MenuAddDTO;
import com.cauli.manage.menu.model.vo.MenuVO;
import com.cauli.manage.menu.model.dto.MenuQueryDTO;
import com.cauli.manage.menu.model.dto.MenuUpdateDTO;
import com.cauli.manage.menu.model.dto.MenuUpdateStatusDTO;
import com.cauli.manage.menu.model.dto.TreeSelectedDTO;
import com.cauli.manage.menu.model.entity.Menu;
import com.cauli.manage.menu.service.MenuService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2022/12/28 14:18
 * @description 菜单 前端控制器
 */
@Api(tags = "菜单")
@ApiSort(2)
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    public static final String CURRENT_MODULE = "菜单";

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取菜单列表")
    @GetMapping("/list")
    public ResultResponse<List<Menu>> listMenu(MenuQueryDTO menuQueryDTO) {
        List<Menu> menuList = menuService.getMenuList(menuQueryDTO);
        return ResultResponse.success().data(menuList);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据菜单ID获取详细信息")
    @GetMapping("/{menuId}")
    public ResultResponse<MenuVO> getMenu(@PathVariable("menuId") Long menuId) {
        MenuVO menuVO = menuService.getMenuById(menuId);
        return ResultResponse.success().data(menuVO);
    }

    @MyLog(operation = "新增菜单", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 3)
    @ApiOperation("新增菜单")
    @PostMapping
    public ResultResponse<Void> addMenu(@Valid @RequestBody MenuAddDTO menuAddDTO) {
        menuService.addMenu(menuAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @MyLog(operation = "修改菜单", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 4)
    @ApiOperation("修改菜单")
    @PutMapping
    public ResultResponse<Void> updateMenu(@Valid @RequestBody MenuUpdateDTO menuUpdateDTO) {
        menuService.updateMenu(menuUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "菜单状修改态", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 5)
    @ApiOperation("菜单状修改态")
    @PutMapping("/status")
    public ResultResponse<Void> changeMenuStatus(@Valid @RequestBody MenuUpdateStatusDTO menuUpdateStatusDTO) {
        menuService.changeMenuStatus(menuUpdateStatusDTO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "删除菜单", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 6)
    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuIds}")
    public ResultResponse<Void> deleteMenu(@PathVariable List<Long> menuIds) {
        menuService.deleteMenu(menuIds);
        return ResultResponse.success().message("删除成功");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("获取用户菜单下拉树列表")
    @GetMapping("/dropdownList")
    public ResultResponse<List<Tree<Long>>> dropdownList() {
        long userId = StpUtil.getLoginIdAsLong();
        List<Tree<Long>> dropdownList = menuService.getDropdownList(userId);
        return ResultResponse.success().data(dropdownList);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping("/roleMenuTreeSelect/{roleId}")
    public ResultResponse<TreeSelectedDTO> roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        long userId = StpUtil.getLoginIdAsLong();
        TreeSelectedDTO roleDropdownList = menuService.getRoleDropdownList(userId, roleId);
        return ResultResponse.success().data(roleDropdownList);
    }
}
